home *** CD-ROM | disk | FTP | other *** search
- /*
- File: SampleSBP2Family.c
-
- Contains: Sample family for Serial Bus Protocol 2 (SBP-2)
-
- Version: 1.0
-
- Copyright: © 1998-1999 by Apple Computer, Inc., all rights reserved.
-
- File Ownership:
-
- DRI: Eric Anderson
-
- Other Contact:
-
- Technology: FireWire
-
- Writers:
-
- (EA) Eric Anderson (ewa)
- (EA) Eric Anderson (ewa)
-
- Change History (most recent first):
-
- <FW3> 1/15/99 EA Fix uninitialized variable problem.
- <FW2> 9/20/98 EA Filled in header comments.
- <FW1> 9/20/98 EA first checked in
- */
-
-
- #include <Types.h>
- #include <Errors.h>
- #include <Strings.h>
- #include <Traps.h>
- #include <Events.h>
- #include <Processes.h>
- #include <AppleEvents.h>
- #include <Devices.h>
- #include <CodeFragments.h>
- #include <DriverServices.h>
- #include <NameRegistry.h>
- #include <GenericDriverFamily.h>
- #include <SampleSBP2.h>
- #include <SampleSBP2Priv.h>
- #include <SampleSBP2Expert.h>
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // Define global structure to hold all of the Sample SBP
- // Family data.
- //
-
- SBPFamilyDataPtr gpSBPFamilyData = nil;
- SBPExpertDataPtr gpSBPExpertData = nil;
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // Internal routine prototypes.
- //
-
- static OSStatus SBPEventHandler (
- GDFDeviceEventDataPtr pGDFDeviceEventData);
-
- static OSStatus SBPHandleDeviceAdded (
- GDFDeviceEventDataPtr pGDFDeviceEventData);
-
- static OSStatus SBPHandleDeviceRemoved (
- GDFDeviceEventDataPtr pGDFDeviceEventData);
-
- static OSErr SBPSendClientAppleEvent (
- SBPClientDataPtr pSBPClientData,
- const AppleEvent *theAppleEvent,
- AESendMode sendMode,
- AESendPriority sendPriority,
- long timeOutInTicks,
- AEIdleUPP idleProc,
- AEFilterUPP filterProc);
-
- static void DisposeClientAppleEventData (
- ClientAppleEventDataPtr pClientAppleEventData);
-
- static OSStatus SBPCreateClientAppleEventQueue (
- SBPClientDataPtr pSBPClientData);
-
- static OSStatus SBPDisposeClientAppleEventQueue (
- SBPClientDataPtr pSBPClientData);
-
- static OSStatus SBPAddDriver (
- GDFDeviceEventDataPtr pGDFDeviceEventData,
- SBPDriverID *pSBPDriverID);
-
- static OSStatus CreateSBPDriverID (
- SBPDriverID *pSBPDriverID);
-
- static void DisposeSBPDriver (
- SBPDriverID sbpDriverID);
-
- static SBPDriverID SBPFindDriver (
- RegEntryIDPtr pRegEntryID);
-
-
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
- //
- // Public routines.
- //
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // RegisterSBPClientApplication
- //
- // This routine registers the calling application as a client of the
- // Sample SBP family.
- //
-
- OSStatus RegisterSBPClientApplication(
- SBPClientID *pSBPClientID,
- UInt32 clientContextData)
- {
- SBPClientID sbpClientID = kInvalidSBPClientID;
- SBPClientDataPtr pSBPClientData = nil,
- pNextSBPClientData;
- ProcessSerialNumber currentPSN;
- OSStatus status = noErr;
-
- // Allocate client data record.
- pSBPClientData =
- (SBPClientDataPtr) PoolAllocateResident (sizeof (SBPClientData), true);
- if (pSBPClientData != nil)
- {
- pSBPClientData->clientContextData = clientContextData;
- sbpClientID = (SBPClientID) pSBPClientData;
- }
- else
- {
- status = memFullErr;
- }
-
- // Add client data record to list.
- if (status == noErr)
- {
- pNextSBPClientData = gpSBPFamilyData->pSBPClientList;
- if (pNextSBPClientData != nil)
- pNextSBPClientData->pPrevSBPClientData = pSBPClientData;
-
- pSBPClientData->pPrevSBPClientData = nil;
- pSBPClientData->pNextSBPClientData = pNextSBPClientData;
- gpSBPFamilyData->pSBPClientList = pSBPClientData;
- }
-
- // Create client Apple Event queue.
- if (status == noErr)
- status = SBPCreateClientAppleEventQueue (pSBPClientData);
-
- // Create an Apple Event target descriptor for the client.
- if (status == noErr)
- {
- status = GetCurrentProcess (¤tPSN);
-
- if (status == noErr)
- {
- status = AECreateDesc (typeProcessSerialNumber,
- ¤tPSN,
- sizeof (ProcessSerialNumber),
- &(pSBPClientData->clientAEAddress));
- if (status == noErr)
- pSBPClientData->clientAEAddressValid = true;
- }
- }
-
- // Create an Apple Event for device added notification.
- if (status == noErr)
- {
- status = AECreateAppleEvent
- (kAESampleSBP2EventClass,
- kAESampleSBP2DeviceAdded,
- &(pSBPClientData->clientAEAddress),
- kAutoGenerateReturnID,
- kAnyTransactionID,
- &(pSBPClientData->deviceAddedAppleEvent));
- if (status == noErr)
- pSBPClientData->deviceAddedAppleEventValid = true;
- }
-
- // Create an Apple Event for device removed notification.
- if (status == noErr)
- {
- status = AECreateAppleEvent
- (kAESampleSBP2EventClass,
- kAESampleSBP2DeviceRemoved,
- &(pSBPClientData->clientAEAddress),
- kAutoGenerateReturnID,
- kAnyTransactionID,
- &(pSBPClientData->deviceRemovedAppleEvent));
- if (status == noErr)
- pSBPClientData->deviceRemovedAppleEventValid = true;
- }
-
- // Clean up on error.
- if (status != noErr)
- {
- if (sbpClientID != kInvalidSBPClientID)
- UnregisterSBPClientApplication (sbpClientID);
- }
-
- // Return results.
- *pSBPClientID = sbpClientID;
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // UnregisterSBPClientApplication
- //
- // This routine unregisters the calling application as a client of the
- // Sample SBP family.
- //
-
- OSStatus UnregisterSBPClientApplication(
- SBPClientID sbpClientID)
- {
- SBPClientDataPtr pSBPClientData,
- pPrevSBPClientData,
- pNextSBPClientData;
- OSStatus status = noErr;
-
- if (sbpClientID != kInvalidSBPClientID)
- {
- // Get client data from ID.
- pSBPClientData = (SBPClientDataPtr) sbpClientID;
-
- // Deallocate device removed Apple Event.
- if (pSBPClientData->deviceRemovedAppleEventValid)
- AEDisposeDesc (&(pSBPClientData->deviceRemovedAppleEvent));
-
- // Deallocate device added Apple Event.
- if (pSBPClientData->deviceAddedAppleEventValid)
- AEDisposeDesc (&(pSBPClientData->deviceAddedAppleEvent));
-
- // Deallocate target descriptor.
- if (pSBPClientData->clientAEAddressValid)
- AEDisposeDesc (&(pSBPClientData->clientAEAddress));
-
- // Dispose of client Apple Event queue.
- if (pSBPClientData->clientAppleEventQueue != nil)
- SBPDisposeClientAppleEventQueue (pSBPClientData);
-
- // Remove client from list.
- pPrevSBPClientData = pSBPClientData->pPrevSBPClientData;
- pNextSBPClientData = pSBPClientData->pNextSBPClientData;
-
- if (pPrevSBPClientData != nil)
- pPrevSBPClientData->pNextSBPClientData = pNextSBPClientData;
- else
- gpSBPFamilyData->pSBPClientList = pNextSBPClientData;
-
- if (pNextSBPClientData != nil)
- pNextSBPClientData->pPrevSBPClientData = pPrevSBPClientData;
-
- // Deallocate client data record.
- PoolDeallocate ((Ptr) pSBPClientData);
- }
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // GetSBPDriverList
- //
- // This routine returns a list of Sample SBP driver IDs and the
- // number of drivers installed in the system.
- //
-
- OSStatus GetSBPDriverList(
- SBPDriverID *pSBPDriverIDList,
- UInt32 sbpDriverIDListSize,
- UInt32 *pNumSBPDrivers)
- {
- SBPDriverDataPtr pSBPDriverData;
- UInt32 numSBPDrivers,
- numSBPDriversInList,
- driverNum;
- OSStatus status = noErr;
-
- // Compute number of drivers to put in list.
- numSBPDrivers = gpSBPFamilyData->numSBPDrivers;
- numSBPDriversInList = sbpDriverIDListSize;
- if (numSBPDriversInList > numSBPDrivers)
- numSBPDriversInList = numSBPDrivers;
-
- // Copy driver IDs in order.
- pSBPDriverData = gpSBPFamilyData->pSBPDriverList;
- for (driverNum = 0; driverNum < numSBPDriversInList; driverNum++)
- {
- pSBPDriverIDList[driverNum] = (SBPDriverID) pSBPDriverData;
- pSBPDriverData = pSBPDriverData->pNextSBPDriverData;
- }
-
- // Return number of installed drivers.
- *pNumSBPDrivers = numSBPDrivers;
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // CallSBPDriver
- //
- // This routine sends a request to the given driver.
- //
-
- OSStatus CallSBPDriver(
- SBPDriverID sbpDriverID,
- SBPInterfaceParamsPtr pSBPInterfaceParams)
- {
- SBPDriverDataPtr pSBPDriverData;
- CntrlParam cntrlParam;
- OSStatus status = noErr;
-
- // Get the driver data from the ID.
- pSBPDriverData = (SBPDriverDataPtr) sbpDriverID;
-
- /*zzz*/
- if (pSBPDriverData->deviceDisconnected)
- return (-139);//zzz need disconnectedErr
- /*zzz*/
- // Set up control parameters.
- cntrlParam.ioCompletion = nil;
- cntrlParam.ioVRefNum = 0;
- cntrlParam.ioCRefNum = pSBPDriverData->driverRefNum;
- cntrlParam.csCode = cscSBPCommand;
- *((Ptr *) &(cntrlParam.csParam[0])) = (Ptr) pSBPInterfaceParams;
-
- // Call the driver.
- status = PBControlSync ((ParmBlkPtr) &cntrlParam);
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // OpenSBPDriver
- //
- // This routine opens a connection to the given driver.
- //
-
- OSStatus OpenSBPDriver (
- SBPDriverID sbpDriverID)
- {
- SBPDriverDataPtr pSBPDriverData;
- OSStatus status = noErr;
-
- // Get the driver data from the ID.
- pSBPDriverData = (SBPDriverDataPtr) sbpDriverID;
-
- // One more connection.
- pSBPDriverData->numConnections++;
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // CloseSBPDriver
- //
- // This routine closes a connection to the given driver.
- //
-
- OSStatus CloseSBPDriver (
- SBPDriverID sbpDriverID)
- {
- SBPDriverDataPtr pSBPDriverData;
- OSStatus status = noErr;
-
- // Get the driver data from the ID.
- pSBPDriverData = (SBPDriverDataPtr) sbpDriverID;
-
- // One less connection.
- pSBPDriverData->numConnections--;
-
- // Dispose driver if device has been disconnected.
- if ((pSBPDriverData->deviceDisconnected) && (pSBPDriverData->numConnections == 0))
- DisposeSBPDriver (sbpDriverID);
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // GetNextSBPClientEvent
- //
- // This routine checks if any client Apple Events are pending and sends any
- // that are.
- //
-
- OSStatus GetNextSBPClientEvent(
- SBPClientID sbpClientID)
- {
- SBPClientDataPtr pSBPClientData;
- ClientAppleEventDataPtr pClientAppleEventData;
- AppleEvent reply;
- OSStatus status = noErr,
- queueStatus = noErr;
-
- // Get client data from ID.
- pSBPClientData = (SBPClientDataPtr) sbpClientID;
-
- // Send any client Apple Events on queue.
- while (queueStatus == noErr)
- {
- queueStatus = PBDequeueFirst (pSBPClientData->clientAppleEventQueue,
- (QElemPtr *) &pClientAppleEventData);
- if (queueStatus == noErr)
- {
- // Send the Apple Event.
- AESend (&(pClientAppleEventData->clientAppleEvent),
- &reply,
- pClientAppleEventData->sendMode,
- pClientAppleEventData->sendPriority,
- pClientAppleEventData->timeOutInTicks,
- pClientAppleEventData->idleProc,
- pClientAppleEventData->filterProc);
-
- // Dispose of reply.
- AEDisposeDesc (&reply);
-
- // We're now done with the client Apple Event data record.
- DisposeClientAppleEventData (pClientAppleEventData);
- }
- }
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
- //
- // Private routines.
- //
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // InitializeSBPFamily
- //
- // This routine initializes the Sample SBP family.
- //
-
- long InitializeSBPFamily(void)
- {
- OSStatus status = noErr;
-
- // Allocate family global record.
- gpSBPFamilyData =
- (SBPFamilyDataPtr) PoolAllocateResident (sizeof (SBPFamilyData), true);
- if (gpSBPFamilyData == nil)
- status = memFullErr;
-
- return ((long) status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // TerminateSBPFamily
- //
- // This routine terminates the Sample SBP family.
- //
-
- long TerminateSBPFamily(void)
- {
- OSStatus status = noErr;
-
- // Deallocate family global record.
- if (gpSBPFamilyData != nil)
- PoolDeallocate ((Ptr) gpSBPFamilyData);
-
- return ((long) status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // InstallSBPExpert
- //
- // This routine installs the Sample SBP expert loading
- // mechanism.
- //
-
- OSStatus InstallSBPExpert(void)
- {
- UInt32 eventTable[1];
- OSStatus status = noErr;
-
- gpSBPExpertData =
- (SBPExpertDataPtr) PoolAllocateResident (sizeof (SBPExpertData), true);
- if (gpSBPExpertData == nil)
- status = memFullErr;
-
- // Register to receive generic device added events.
- if (status == noErr)
- {
- eventTable[0] = kGDFDeviceAddedEvent;
- eventTable[1] = kGDFDeviceRemovedEvent;
- status = GDFRegisterDeviceEventHandlerProc
- (kNdrvTypeIsSampleSBP2,
- 2,
- &eventTable[0],
- SBPEventHandler,
- nil,
- &(gpSBPExpertData->gdfDeviceEventRegistrationID));
- }
-
- return status;
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // UninstallSBPExpert
- //
- // This routine uninstalls the Sample SBP expert loading
- // mechanism.
- //
-
- OSStatus UninstallSBPExpert(void)
- {
- OSStatus status = noErr;
-
- // Dispose of SBPExpert data.
- if (gpSBPExpertData != nil)
- {
- // Unregister with Generic Device family.
- if (gpSBPExpertData->gdfDeviceEventRegistrationID !=
- kInvalidGDFDeviceEventRegistrationID)
- {
- status = GDFUnregisterDeviceEventHandler
- (gpSBPExpertData->gdfDeviceEventRegistrationID);
- }
-
- PoolDeallocate ((Ptr) gpSBPExpertData);
- gpSBPExpertData = nil;
- }
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
- //
- // Internal routines.
- //
- ////////////////////////////////////////////////////////////////////////////////
- ////////////////////////////////////////////////////////////////////////////////
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // SBPEventHandler
- //
- // This proc handles device events for Sample SBP devices.
- //
-
- static OSStatus SBPEventHandler(
- GDFDeviceEventDataPtr pGDFDeviceEventData)
- {
- OSStatus status = noErr;
-
- // Dispatch off of device event.
- switch (pGDFDeviceEventData->deviceEvent)
- {
- case kGDFDeviceAddedEvent :
- status = SBPHandleDeviceAdded (pGDFDeviceEventData);
- break;
-
- case kGDFDeviceRemovedEvent :
- status = SBPHandleDeviceRemoved (pGDFDeviceEventData);
- break;
-
- default :
- break;
- }
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // SBPHandleDeviceAdded
- //
- // This routine handles new devices being added.
- //
-
- static OSStatus SBPHandleDeviceAdded(
- GDFDeviceEventDataPtr pGDFDeviceEventData)
- {
- SBPDriverID SBPDriverID;
- SBPDriverDataPtr pSBPDriverData;
-
- SBPClientDataPtr pSBPClientData;
-
- OSStatus status = noErr,
- clientStatus;
-
- // Check if SBP driver is in our list. This will happen if device was
- // connected, opened, disconnected, and reconnected without being closed.
- // If it's in our list, set disconnected to false.
- SBPDriverID = SBPFindDriver (&(pGDFDeviceEventData->deviceRegEntryID));
- if (SBPDriverID != kInvalidSBPDriverID)
- {
- pSBPDriverData = (SBPDriverDataPtr) SBPDriverID;
- pSBPDriverData->deviceDisconnected = false;
- }
- else
- {
- status = SBPAddDriver (pGDFDeviceEventData, &SBPDriverID);
- if (status == noErr)
- pSBPDriverData = (SBPDriverDataPtr) SBPDriverID;
- }
-
- // Send up notification to all clients.
- if (status == noErr)
- {
- pSBPClientData = gpSBPFamilyData->pSBPClientList;
- while (pSBPClientData != nil)
- {
- // Add the driver ID parameter to event.
- clientStatus = AEPutParamPtr
- (&(pSBPClientData->deviceAddedAppleEvent),
- kAESBPDriverIDKey,
- kAESBPDriverIDType,
- &SBPDriverID,
- sizeof (SBPDriverID));
-
- // Send event.
- if (clientStatus == noErr)
- {
- clientStatus = SBPSendClientAppleEvent
- (pSBPClientData,
- &(pSBPClientData->deviceAddedAppleEvent),
- 0,
- kAENormalPriority,
- 0,
- nil,
- nil);
- }
-
- pSBPClientData = pSBPClientData->pNextSBPClientData;
- }
- }
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // SBPHandleDeviceRemoved
- //
- // This routine handles devices being removed.
- //
-
- static OSStatus SBPHandleDeviceRemoved(
- GDFDeviceEventDataPtr pGDFDeviceEventData)
- {
- RegEntryIDPtr pRegEntryID;
- SBPDriverID sbpDriverID;
- SBPDriverDataPtr pSBPDriverData;
- SBPClientDataPtr pSBPClientData;
- Boolean found;
- OSStatus status = noErr,
- clientStatus;
-
- // Get Name Registry entry.
- pRegEntryID = &(pGDFDeviceEventData->deviceRegEntryID);
-
- // Find driver that is being removed.
- sbpDriverID = SBPFindDriver (pRegEntryID);
- if (sbpDriverID != kInvalidSBPDriverID)
- {
- pSBPDriverData = (SBPDriverDataPtr) sbpDriverID;
- found = true;
- }
- else
- {
- found = false;
- }
-
- // If driver was found, set it disconnected.
- if (found)
- pSBPDriverData->deviceDisconnected = true;
-
- // Dispose driver if no one has it open.
- // Otherwise, send up notification to all clients.
- if (found)
- {
- if (pSBPDriverData->numConnections == 0)
- {
- DisposeSBPDriver ((SBPDriverID) pSBPDriverData);
- }
- else
- {
- pSBPClientData = gpSBPFamilyData->pSBPClientList;
- while (pSBPClientData != nil)
- {
- // Add the driver ID parameter to event.
- clientStatus = AEPutParamPtr
- (&(pSBPClientData->deviceRemovedAppleEvent),
- kAESBPDriverIDKey,
- kAESBPDriverIDType,
- &sbpDriverID,
- sizeof (SBPDriverID));
-
- // Send event.
- if (clientStatus == noErr)
- {
- clientStatus = SBPSendClientAppleEvent
- (pSBPClientData,
- &(pSBPClientData->deviceRemovedAppleEvent),
- 0,
- kAENormalPriority,
- 0,
- nil,
- nil);
- }
-
- pSBPClientData = pSBPClientData->pNextSBPClientData;
- }
- }
- }
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // SBPSendClientAppleEvent
- //
- // This routine queues an Apple Event to be sent to a client for device
- // notification.
- //
-
- static OSErr SBPSendClientAppleEvent(
- SBPClientDataPtr pSBPClientData,
- const AppleEvent *theAppleEvent,
- AESendMode sendMode,
- AESendPriority sendPriority,
- long timeOutInTicks,
- AEIdleUPP idleProc,
- AEFilterUPP filterProc)
- {
- ClientAppleEventDataPtr pClientAppleEventData = nil;
- OSErr err = noErr;
-
- // Create data for the client Apple Event.
- pClientAppleEventData = (ClientAppleEventDataPtr)
- PoolAllocateResident (sizeof (ClientAppleEventData), true);
- if (pClientAppleEventData != nil)
- {
- pClientAppleEventData->sendMode =
- (sendMode & ~(kAENoReply | kAEQueueReply | kAEWaitReply)) | kAENoReply;
- pClientAppleEventData->sendPriority = sendPriority;
- pClientAppleEventData->timeOutInTicks = timeOutInTicks;
- pClientAppleEventData->idleProc = idleProc;
- pClientAppleEventData->filterProc = filterProc;
- }
- else
- {
- err = memFullErr;
- }
-
- // Copy the given Apple Event.
- if (err == noErr)
- {
- err = AEDuplicateDesc (theAppleEvent,
- &(pClientAppleEventData->clientAppleEvent));
- if (err == noErr)
- pClientAppleEventData->clientAppleEventValid = true;
- }
-
- // Queue the client Apple Event to be sent at a good time.
- if (err == noErr)
- {
- err = PBEnqueueLast ((QElemPtr) pClientAppleEventData,
- pSBPClientData->clientAppleEventQueue);
- }
-
- // Clean up on error.
- if (err != noErr)
- DisposeClientAppleEventData (pClientAppleEventData);
-
- return (err);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // DisposeClientAppleEventData
- //
- // This routine disposes of the client Apple Event record.
- //
-
- static void DisposeClientAppleEventData(
- ClientAppleEventDataPtr pClientAppleEventData)
- {
- if (pClientAppleEventData != nil)
- {
- // Dispose of Apple Event copy.
- if (pClientAppleEventData->clientAppleEventValid)
- AEDisposeDesc (&(pClientAppleEventData->clientAppleEvent));
-
- // Dispose of client Apple Event data record.
- PoolDeallocate ((Ptr) pClientAppleEventData);
- }
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // SBPCreateClientAppleEventQueue
- //
- // This routine creates a client AppleEvent queue.
- //
-
- static OSStatus SBPCreateClientAppleEventQueue(
- SBPClientDataPtr pSBPClientData)
- {
- OSStatus status = noErr;
-
- status = PBQueueCreate (&(pSBPClientData->clientAppleEventQueue));
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // SBPDisposeClientAppleEventQueue
- //
- // This routine disposes of a client AppleEvent queue.
- //
-
- static OSStatus SBPDisposeClientAppleEventQueue(
- SBPClientDataPtr pSBPClientData)
- {
- ClientAppleEventDataPtr pClientAppleEventData;
- OSStatus status = noErr;
-
- if (pSBPClientData->clientAppleEventQueue != nil)
- {
- // Dispose of any client AppleEvents still in queue.
- while (status == noErr)
- {
- status = PBDequeueFirst (pSBPClientData->clientAppleEventQueue,
- (QElemPtr *) &pClientAppleEventData);
- if (status == noErr)
- DisposeClientAppleEventData (pClientAppleEventData);
- }
-
- // Delete queue.
- PBQueueDelete (pSBPClientData->clientAppleEventQueue);
- }
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // SBPAddDriver
- //
- // This routine adds a new driver.
- //
-
- static OSStatus SBPAddDriver(
- GDFDeviceEventDataPtr pGDFDeviceEventData,
- SBPDriverID *pSBPDriverID)
- {
- RegEntryIDPtr pRegEntryID;
- SBPDriverID sbpDriverID = kInvalidSBPDriverID;
- SBPDriverDataPtr pSBPDriverData;
-
- OSStatus status = noErr;
-
- // Get Name Registry entry.
- pRegEntryID = &(pGDFDeviceEventData->deviceRegEntryID);
-
- // Add driver record to list.
- if (status == noErr)
- status = CreateSBPDriverID (&sbpDriverID);
-
- // Get the driver data from the ID.
- if (status == noErr)
- pSBPDriverData = (SBPDriverDataPtr) sbpDriverID;
-
- // Fill in driver data record.
- if (status == noErr)
- {
- pSBPDriverData->deviceRegistryID = *pRegEntryID;
- pSBPDriverData->driverRefNum = pGDFDeviceEventData->driverRefNum;
- }
-
- // Open driver.
- if (status == noErr)
- {
- status = OpenInstalledDriver (pGDFDeviceEventData->driverRefNum, fsRdWrPerm);
- if (status == noErr)
- pSBPDriverData->driverOpened = true;
- }
-
- // Clean up on error.
- if (status != noErr)
- {
- if (sbpDriverID != kInvalidSBPDriverID)
- DisposeSBPDriver (sbpDriverID);
- }
-
- // Return results.
- if (status == noErr)
- *pSBPDriverID = sbpDriverID;
- else
- *pSBPDriverID = kInvalidSBPDriverID;
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // CreateSBPDriverID
- //
- // This routine creates a new Sample SBP driver record, adds it to
- // the family list, and returns the ID for the new driver record.
- //
-
- static OSStatus CreateSBPDriverID(
- SBPDriverID *pSBPDriverID)
- {
- SBPDriverDataPtr pSBPDriverData = nil,
- pNextSBPDriverData;
- OSStatus status = noErr;
-
- // Allocate Sample SBP driver record.
- pSBPDriverData = (SBPDriverDataPtr)
- PoolAllocateResident (sizeof (SBPDriverData), true);
- if (pSBPDriverData != nil)
- pSBPDriverData->sbpDriverID = (SBPDriverID) pSBPDriverData;
- else
- status = memFullErr;
-
- // Add driver record to family list.
- if (status == noErr)
- {
- pNextSBPDriverData = gpSBPFamilyData->pSBPDriverList;
- pSBPDriverData->pNextSBPDriverData = pNextSBPDriverData;
- gpSBPFamilyData->pSBPDriverList = pSBPDriverData;
- gpSBPFamilyData->numSBPDrivers++;
- }
-
- // Return result and clean up on error.
- if (status == noErr)
- {
- *pSBPDriverID = pSBPDriverData->sbpDriverID;
- }
- else
- {
- if (pSBPDriverData != nil)
- PoolDeallocate ((Ptr) pSBPDriverData);
-
- *pSBPDriverID = kInvalidSBPDriverID;
- }
-
- return (status);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // DisposeSBPDriver
- //
- // This routine disposes of the given driver. It will call the driver to
- // terminate itself, dispose of the driver memory closure, remove the driver
- // data record from our list, and dispose of the driver data record.
- //
-
- static void DisposeSBPDriver(
- SBPDriverID sbpDriverID)
- {
- SBPDriverDataPtr pSBPDriverData,
- pSearchSBPDriverData,
- pPrevSBPDriverData;
- THz currentZone;
-
- // Switch to system zone.
- currentZone = GetZone ();
- SetZone (SystemZone ());
-
- // Get the driver data from the ID.
- pSBPDriverData = (SBPDriverDataPtr) sbpDriverID;
-
- // Close the driver.
- if (pSBPDriverData->driverOpened)
- CloseDriver (pSBPDriverData->driverRefNum);
-
- // Remove driver record from family list.
- pSearchSBPDriverData = gpSBPFamilyData->pSBPDriverList;
- pPrevSBPDriverData = nil;
- while ((pSearchSBPDriverData != nil) &&
- (pSearchSBPDriverData != pSBPDriverData))
- {
- pPrevSBPDriverData = pSearchSBPDriverData;
- pSearchSBPDriverData =
- pSearchSBPDriverData->pNextSBPDriverData;
- }
- if (pSearchSBPDriverData != nil)
- {
- if (pPrevSBPDriverData != nil)
- {
- pPrevSBPDriverData->pNextSBPDriverData =
- pSBPDriverData->pNextSBPDriverData;
- }
- else
- {
- gpSBPFamilyData->pSBPDriverList =
- pSBPDriverData->pNextSBPDriverData;
- }
- }
- gpSBPFamilyData->numSBPDrivers--;
-
- // Deallocate driver data record.
- PoolDeallocate ((Ptr) pSBPDriverData);
-
- // Switch zone back.
- SetZone (currentZone);
- }
-
-
- ////////////////////////////////////////////////////////////////////////////////
- //
- // SBPFindDriver
- //
- // This routine finds the SBP driver corresponding to the given Name Registry
- // entry.
- //
-
- static SBPDriverID SBPFindDriver(
- RegEntryIDPtr pRegEntryID)
- {
- SBPDriverID sbpDriverID;
- SBPDriverDataPtr pSBPDriverData;
- Boolean found;
-
- // Find driver that has matching name registry entry.
- pSBPDriverData = gpSBPFamilyData->pSBPDriverList;
- found = false;
- while ((pSBPDriverData != nil) && (!found))
- {
- if (RegistryEntryIDCompare (&(pSBPDriverData->deviceRegistryID), pRegEntryID))
- found = true;
- else
- pSBPDriverData = pSBPDriverData->pNextSBPDriverData;
- }
-
- // Set return value.
- if (found)
- sbpDriverID = (SBPDriverID) pSBPDriverData;
- else
- sbpDriverID = kInvalidSBPDriverID;
-
- return (sbpDriverID);
- }
-